#!/bin/bash
echo "Executing script by: LIUCHANGXING 24055060146"
echo "-----------------------------------------"
SOURCE_FILE="scores.txt"
OUTPUT_DIR="students"
if [ ! -f "$SOURCE_FILE" ]; then
    echo "Error: Source file '$SOURCE_FILE' not found!"
    exit 1
fi
mkdir -p "$OUTPUT_DIR"
while read name gender id java_subj java_score python_subj python_score cpp_subj cpp_score linux_subj linux_score; do
    total=$(( java_score + python_score + cpp_score + linux_score ))
    average=$(echo "scale=1; $total / 4" | bc)
    student_file="${OUTPUT_DIR}/${name}.txt"
    echo "name: $name" > "$student_file"
    echo "id: $id" >> "$student_file"
    echo "gender: $gender" >> "$student_file"
    echo "scores: $java_subj $java_score $python_subj $python_score $cpp_subj $cpp_score $linux_subj $linux_score" >> "$student_file"
    echo "total: $total" >> "$student_file"
    echo "average: $average" >> "$student_file"
    echo "Created file: $student_file"
done < "$SOURCE_FILE"
echo "-----------------------------------------"
echo "All student files have been created in the '$OUTPUT_DIR' directory."
exit 0
